package com.woniuxy.merchant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.NoticeListDto;
import com.woniu.form.PublishNoticeForm;
import com.woniu.form.QueryNoticeForm;
import com.woniu.form.UpdateNoticeForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniuxy.merchant.service.INotisService;
import com.woniu.commons.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/notis")
@Api(tags = "公告模块")
public class NotisController {
    @Resource
    private INotisService notisService;
    @Autowired
    private TokenUtil tokenUtil;
    @ApiOperation(value = "公告发布")
    @PostMapping("/publish")
    public Result<String> publishNotice(@Valid @RequestBody PublishNoticeForm publishNoticeForm,
                                        @RequestHeader("token") String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        notisService.publishNotice(publishNoticeForm, adminId);
        return Result.getResult(String.class).setCode(200).setMsg("公告发布成功");
    }
    @ApiOperation(value = "公告集合分页查询")
    @GetMapping("/list")
    public Result<Page<NoticeListDto>> noticeList(QueryNoticeForm queryNoticeForm){
        Page<NoticeListDto> page = notisService.noticeList(queryNoticeForm);
        return new Result<Page<NoticeListDto>>().setCode(200).setMsg("查询公告成功").setData(page);
    }
    @ApiOperation(value = "删除公告")
    @GetMapping("/delete")
    public Result deleteNotice(Integer noticeId,@RequestHeader("token") String token){
        Integer adminId = tokenUtil.getAdminId(token);
        boolean delete = notisService.deleteNotice(noticeId);
        if (delete){
            return new Result().setCode(200).setMsg("删除成功");
        }else {
            return new Result().setCode(500).setMsg("删除失败");
        }
    }

    @ApiModelProperty(value = "修改公告")
    @PostMapping("/updateNotice")
    public Result updateNotice(@RequestBody UpdateNoticeForm updateNoticeForm, @RequestHeader("token") String token){
        Integer id = tokenUtil.getAdminId(token);
        if (notisService.updateNotice(updateNoticeForm)){
            return new Result().setCode(200).setMsg("修改成功");
        }else {
            return new Result().setCode(500).setMsg("修改失败");
        }
    }

}
